package com.qkst.website.backstage.controller;


import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.qkst.website.backstage.bean.ProductBean;
import com.qkst.website.backstage.service.ProductService;
import com.qkst.website.common.core.BaseController;

/**
 * @author lh
 * @description 产品类别控制器
 * @date 2017-05-03
 * @email ahlxlihao@163.com
 *
 */
@Controller
@RequestMapping("/backstage/product")
public class ProductController extends BaseController{

	@Autowired
	private ProductService productService;

	/**
	 * 查询产品
	 * @return
	 * @throws IOException 
	 */
	@RequestMapping(value = "/list.do", method = { RequestMethod.GET })
	public ModelAndView list(ProductBean product) throws IOException{
		ModelAndView mv=this.backstageModelAndView("product/list");
		List<ProductBean> productList = productService.list(product);
		mv.addObject("productList", productList);
		return mv;

	}
	
	/**
	 * 添加产品
	 * @return
	 * @throws IOException 
	 */
	@RequestMapping(value = "/add.html", method = { RequestMethod.GET })
	public ModelAndView add_html(HttpServletRequest request, HttpServletResponse response) throws IOException{
		ModelAndView mv=this.backstageModelAndView("product/add");
		return mv;
	}
	
	/**
	 * 跳转至产品添加页面
	 * @return
	 */
	@RequestMapping(value = "/add.do", method = { RequestMethod.POST })
	@ResponseBody
	public Map<String, Object> add(@RequestBody ProductBean product){
		Map<String, Object> result = new HashMap<String, Object>();
		//插入产品信息
		product=productService.save(product);
		result.put("product", product);
		return result;

	}
	
	/**
	 * 删除产品
	 * @return
	 */
	@RequestMapping(value = "/delete.do", method = { RequestMethod.POST })
	@ResponseBody
	public Map<String, Object>  delete(ProductBean product){
		Map<String, Object> result = new HashMap<String, Object>();
		boolean flag=productService.delete(product);
		result.put("isSuccess", flag);
		result.put("product ", product);
		return result;

	}
	

}
